﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Logging;
using Newtonsoft.Json;
using OutDbContext;

namespace Out.Service.Services
{
    public class BizPaper : IDisposable
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(BizPaper));

        private static readonly object OrderNoLocker = new object();

        private readonly GdsfybjyWeiXinEntities _ctx;

        private readonly string _openId;

        public BizPaper(string openId)
        {
            _ctx = new GdsfybjyWeiXinEntities();
            _openId = openId;
        }

        /// <summary>
        /// 判断满意度问卷是否存在
        /// </summary>
        /// <param name="paperId"></param>
        /// <returns></returns>
        public bool InvestigatePaperExists(int paperId)
        {
            return _ctx.SIWaitInvestigatePapers.Any(x => x.Id == paperId && x.Status == 0);

        }
        /// <summary>
        /// 获取满意度调查答案
        /// </summary>
        /// <param name="pagerId"></param>
        /// <returns></returns>
        public List<SIUserAnswer> UserAnswers(int pagerId)
        {
            return _ctx.SIUserAnswers.Where(x => x.PaperId == pagerId).ToList();
        }

        /// <summary>
        /// 获取满意度调查列表
        /// </summary>
        /// <returns></returns>
        public List<PaperModel> InvestigatePaperList()
        {

          return  (from sps in _ctx.SIWaitInvestigatePapers
             join hcs in _ctx.HospCards on sps.PatientId equals hcs.PatientId into temp
             from tt in temp.DefaultIfEmpty()
             where tt.OpenId == _openId && sps.Status == 0 && tt.DeleteStatus == 0 && tt.CardStatus == 1
             select new PaperModel
             {
                 Id = sps.Id,
                 CreateTime = sps.CreateTime,
                 CardNo = sps.CardNo,
                 Name = sps.Name,
                 OpenId = tt.OpenId,
             }).ToList();
        }

        /// <summary>
        /// 获取10个已完成满意度调查
        /// </summary>
        /// <returns></returns>
        public List<SIWaitInvestigatePaper> CompletedPaperList()
        {
           return _ctx.SIWaitInvestigatePapers.Where(x => x.SubmitOpenId == _openId && (x.Status & 0x1) == 0x1).Take(10).ToList();
        }

        /// <summary>
        /// 保存满意度调查数据
        /// </summary>
        /// <param name="paperId">调查Id</param>
        /// <param name="socre1">分数1</param>
        /// <param name="socre2">分数2</param>
        /// <param name="socre3">分数3</param>
        /// <param name="socre4">分数4</param>
        /// <param name="socre5">分数5</param>
        /// <param name="socre6">分数6</param>
        /// <param name="answer">答案</param>
        /// <param name="phone">电话</param>
        /// <returns></returns>
        public ResResult<string> SubmitPingJia(int paperId, int socre1, int socre2, int socre3, int socre4, int socre5, int socre6, string answer, string phone)
        {
            var result = new ResResult<string>(Result.成功);
            try
            {
                SIUserAnswer sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = socre1.ToString(),
                    Question = "FuWuTaiDu",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = socre2.ToString(),
                    Question = "JiuZhenLiuChen",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = socre3.ToString(),
                    Question = "DengHouShiJian",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = socre4.ToString(),
                    Question = "HuanJinWeiSheng",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = socre5.ToString(),
                    Question = "YiDeYiFeng",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = socre6.ToString(),
                    Question = "ZhongTiPingJia",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = answer,
                    Question = "就医体验",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                sa = new SIUserAnswer()
                {
                    AddDt = DateTime.Now,
                    Answer = phone,
                    Question = "联系方式",
                    PaperId = paperId,
                };
                _ctx.SIUserAnswers.Add(sa);
                var paper = _ctx.SIWaitInvestigatePapers.FirstOrDefault(x => x.Id == paperId);
                if (paper != null)
                {
                    paper.Status = 1;
                    paper.SubmitCount = paper.SubmitCount + 1;
                    paper.SubmitOpenId = _openId;
                    paper.SubmitTime = DateTime.Now;
                }
                _ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("保存满意度调查数据出错,信息：{0}", JsonConvert.SerializeObject(ex));
                result.Result = Result.失败;
                result.Message = "保存满意度调查数据出错："+ex.Message;
                return result;
            }

            return result;
        }

        /// <summary>
        /// 资源释放
        /// </summary>
        public void Dispose()
        {
            _ctx.Dispose();
        }
    }

    public class PaperModel
    {
        public int Id { get; set; }

        public DateTime? CreateTime { get; set; }

        public string CardNo { get; set; }

        public string Name { get; set; }

        public string OpenId { get; set; }
    }
}
